package io.renren.common.aspect;

import io.renren.common.exception.FjException;
import io.renren.common.response.BaseRequest;
import io.renren.common.response.Response;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 分拣系统 处理 时间 和 方法名称
 * create by znr48268 on 2018/12/11
 */
@Aspect
@Component
public class BasicReqAspect {


    @Pointcut("@annotation(io.renren.common.annotation.WebApi)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {

        try {

            String ts = "0";
            String method = null;

            Object[] args = point.getArgs();

            for (Object arg : args) {
                if (arg instanceof BaseRequest) {
                    ts = ((BaseRequest) arg).getTs();
                    method = ((BaseRequest) arg).getMethod();
                }
            }
            Object result = point.proceed();
            if (result instanceof Response) {
                ((Response) result).setTs(ts);
                ((Response) result).setMethod(method);
            }

            return result;
        } catch (Exception e) {
            FjException fj = new FjException("系统异常，请联系管理员", -1);
            throw fj;
        }

    }


}
